
package jzService.msg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SoftFan.Log.wf_Log;
import SoftFan.util.systemUtil;
import SoftFan.util.textUnit;

public class EquipmentAssociateMgr {

	private static EquipmentAssociateMgr	instance	= new EquipmentAssociateMgr();

	public static EquipmentAssociateMgr getInstance() {
		return instance;
	}

	private List<EquipmentAssociateData>			associates	= new ArrayList<EquipmentAssociateData>();

	private HashMap<String, EquipmentAssociateData>	macMap		= new HashMap<String, EquipmentAssociateData>();
	private HashMap<String, EquipmentAssociateData>	macIpMap		= new HashMap<String, EquipmentAssociateData>();
	
	private HashMap<String, EquipmentAssociateData>	chairMap	= new HashMap<String, EquipmentAssociateData>();
	private HashMap<String, EquipmentAssociateData>	chairIpMap	= new HashMap<String, EquipmentAssociateData>();

	private EquipmentAssociateData buildEqu(jz.ct.sbpd.location loc) {
		EquipmentAssociateData data;
		data = new EquipmentAssociateData();
		data.setMacId(loc.getMacId());
		data.setChairId(loc.getChairId());
		data.setMacIp(loc.getMacip());
		data.setChairIp(loc.getChairip());
		data.setMacCode(loc.getMaccode());
		data.setChairCode(loc.getChaircode());
		associates.add(data);
		macMap.put(data.getMacCode(), data);
		chairMap.put(data.getChairCode(), data);
		if (!textUnit.StringIsEmpty(data.getMacIp())) {
			macIpMap.put(data.getMacIp(), data);
		}
		if (!textUnit.StringIsEmpty(data.getChairIp())) {
			chairIpMap.put(data.getChairIp(), data);
		}
		return data;
	}
	
	public EquipmentAssociateData getByMac(String code) {
		synchronized (this) {
			EquipmentAssociateData data = macMap.get(code);
			if (data == null) {
				try {
					jz.ct.sbpd.location loc = jz.ct.sbpd.locationJDBCUnit.getInstance().findUniqueByAssmacuq(code);
					if (loc == null) {
						return null;
					}
					data = buildEqu(loc);
				} catch (java.lang.Exception e) {
					wf_Log.sys_log(systemUtil.getErrorMessage(e));
					return null;
				}
			}
			return data;
		}
	}

	public EquipmentAssociateData getByMacIp(String ip) {
		synchronized (this) {
			EquipmentAssociateData data = macIpMap.get(ip);
			if (data == null) {
				try {
					jz.ct.sbpd.location loc = jz.ct.sbpd.locationJDBCUnit.getInstance().findUniqueByMacIp(ip);
					if (loc == null) {
						return null;
					}
					data = buildEqu(loc);
				} catch (java.lang.Exception e) {
					wf_Log.sys_log(systemUtil.getErrorMessage(e));
					return null;
				}
			}
			return data;
		}
	}

	public EquipmentAssociateData getByChair(String code) {
		synchronized (this) {
			EquipmentAssociateData data = chairMap.get(code);
			if (data == null) {
				try {
					jz.ct.sbpd.location loc = jz.ct.sbpd.locationJDBCUnit.getInstance().findUniqueByAsschairuq(code);
					if (loc == null) {
						return null;
					}
					data = buildEqu(loc);
				} catch (java.lang.Exception e) {
					wf_Log.sys_log(systemUtil.getErrorMessage(e));
					return null;
				}
			}
			return data;
		}
	}
	
	public EquipmentAssociateData getByChairIp(String ip) {
		synchronized (this) {
			EquipmentAssociateData data = chairIpMap.get(ip);
			if (data == null) {
				try {
					jz.ct.sbpd.location loc = jz.ct.sbpd.locationJDBCUnit.getInstance().findUniqueByAsschairuq(ip);
					if (loc == null) {
						return null;
					}
					data = buildEqu(loc);
				} catch (java.lang.Exception e) {
					wf_Log.sys_log(systemUtil.getErrorMessage(e));
					return null;
				}
			}
			return data;
		}
	}
}
